/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import sirtet.Piece;

/**
 * A panel that can show a piece
 * @author guangwei.zhu
 */
public abstract class PiecePanel extends JPanel {

    public PiecePanel() {
        initComponents();
    }

    protected void initComponents() {
        this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        this.setBackground(Color.WHITE);

        this.blockSize = new Dimension(20, 20);
    }

    public void setBlockSize(Dimension blockSize) {
        this.blockSize = blockSize;
        this.repaint();
    }

    public void setPiece(Piece piece) {
        this.piece = piece;
        this.repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        Point point = new Point();
        Point origin = new Point();
        int i, j;

        g.setColor(this.getBackground());
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        
        switch (horizontalAlignment) {
            case SwingConstants.LEFT:
                origin.x = 0;
                break;
            case SwingConstants.CENTER:
                origin.x = (this.getWidth() - blockSize.width * piece.getWidth()) / 2;
                break;
            case SwingConstants.RIGHT:
                origin.x = this.getWidth() - blockSize.width * piece.getWidth();
                break;
        }

        switch (verticalAlignment) {
            case SwingConstants.TOP:
                origin.y = 0;
                break;
            case SwingConstants.CENTER:
                origin.y = (this.getHeight() - blockSize.height * piece.getHeight()) / 2;
                break;
            case SwingConstants.BOTTOM:
                origin.y = this.getHeight() - blockSize.height * piece.getHeight();
                break;
        }

        for (i = 0; i < piece.getHeight(); i++) {
            for (j = 0; j < piece.getWidth(); j++) {
                point.x = j * blockSize.width + origin.x;
                point.y = i * blockSize.height + origin.y;
                renderBlock(g, piece.getElement(j, i), point, blockSize);
            }
        }
    }

    public void setHorizontalAlignment(int alignment) {
        switch (alignment) {
            case SwingConstants.LEFT:
            case SwingConstants.CENTER:
            case SwingConstants.RIGHT:
                horizontalAlignment = alignment;
                break;
            default:
                throw new IllegalArgumentException();
        }

        this.repaint();
    }

    public void setVerticalAlignment(int alignment) {
        switch (alignment) {
            case SwingConstants.TOP:
            case SwingConstants.CENTER:
            case SwingConstants.BOTTOM:
                verticalAlignment = alignment;
                break;
            default:
                throw new IllegalArgumentException();
        }

        this.repaint();
    }

    protected int horizontalAlignment = SwingConstants.LEFT;
    protected int verticalAlignment = SwingConstants.TOP;

    protected abstract void renderBlock(Graphics g, Object block, Point point, Dimension size);
    protected Piece piece;
    protected Dimension blockSize;
}
